﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Printing;

namespace Microsoft.Samples
{
    public class PrintBehavior : TargetedTriggerAction<UIElement>
    {
        protected override void Invoke(object parameter)
        {
            PrintDocument printDocument = new PrintDocument();
            printDocument.PrintPage += new EventHandler<PrintPageEventArgs>(printDocument_PrintPage);
            printDocument.Print(DocumentName);
        }

        void printDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            PrintDocument printDocument = sender as PrintDocument;
            printDocument.PrintPage -= printDocument_PrintPage;

            e.PageVisual = this.Target;
        }

        public string DocumentName
        {
            get { return (string)GetValue(DocumentNameProperty); }
            set { SetValue(DocumentNameProperty, value); }
        }

        public static readonly DependencyProperty DocumentNameProperty =
            DependencyProperty.Register(
                "DocumentName", 
                typeof(string), typeof(PrintBehavior), 
                new PropertyMetadata("Document name"));
        
    }
}
